package com.zl.tby.controller;

import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyLayerConfigQuery;
import com.zl.tby.dto.TbyLayerConfigQuery;
import com.zl.tby.model.TbyLayerConfig;
import com.zl.tby.service.ITbyLayerConfigService;
import com.zl.tby.vo.TbyLayerConfigForm;
import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;

import java.util.List;

/**
 * 地图图层配置
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Valid
@Controller
@Mapping("/layer_config")
public class TbyLayerConfigController {

    @Inject
    private ITbyLayerConfigService tbyLayerConfigService;

    /**
     * 保存或新增
     * @param form: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/save", method = MethodType.POST)
    public R<TbyLayerConfig> saveOrUpdate(@Validated TbyLayerConfigForm form) {
        try {
            if (form.getId() == null) {
                return tbyLayerConfigService.add(form);
            }
            return tbyLayerConfigService.update(form);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 删除数据
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/delete", method = MethodType.DELETE)
    public R<Void> delete(@NotNull Long id) {
        return tbyLayerConfigService.delete(id);
    }

    /**
     * 根据id查询
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/get", method = MethodType.GET)
    public R<TbyLayerConfig> get(@NotNull Long id) {
        TbyLayerConfig entity = this.tbyLayerConfigService.get(id);
        return entity == null ? R.failed("数据不存在") : R.ok(entity);
    }

    /**
     * 分页查询
     * @param query: 查询参数
     * @return 分页结果
     */
    @Mapping(value = "/page", method = MethodType.GET)
    public R<?> page(@Body TbyLayerConfigQuery query) {
        return R.ok(this.tbyLayerConfigService.page(query));
    }

    /**
     * 列表查询
     * @param query: 查询参数
     * @return 列表结果
     */
    @Mapping(value = "/list", method = MethodType.GET)
    public R<List<TbyLayerConfig>> List(@Body TbyLayerConfigQuery query) {
        return R.ok(this.tbyLayerConfigService.list(query));
    }

    /**
     * 设置使用或取消
     * @param config：参数
     * @return 结果
     */
    @Mapping(value = "/active", method = MethodType.POST)
    public R<Void> active(TbyLayerConfig config) {
        if (config.getId() == null || config.getActive() == null || (config.getActive().compareTo(0) != 0 && config.getActive().compareTo(1) != 0)) {
            return R.failed("非法参数");
        }
        return this.tbyLayerConfigService.active(config.getId(), config.getActive());
    }

    /**
     * 大屏获取底图配置
     * @return 结果
     */
    @Mapping(value = "/active_data", method = MethodType.GET)
    public R<TbyLayerConfig> activeData() {
        TbyLayerConfigQuery query = new TbyLayerConfigQuery();
        query.setActive(1);
        List<TbyLayerConfig> dataList = this.tbyLayerConfigService.list(query);
        if (dataList == null || dataList.size() == 0) {
            return R.failed("未配置底图参数");
        }
        return R.ok(dataList.get(0));
    }

    /**
     * 验证图层名是否重复
     * @param query: 查询参数
     * @return 结果
     */
    @Mapping(value = "/exist_name", method = MethodType.GET)
    public R<Boolean> existName(@Body TbyLayerConfigQuery query) {
        return this.tbyLayerConfigService.checkName(query.getId(), query.getName());
    }

    /**
     * 验证图层地址是否重复
     * @param query: 查询参数
     * @return 结果
     */
    @Mapping(value = "/exist_url", method = MethodType.GET)
    public R<Boolean> existUrl(@Body TbyLayerConfigQuery query) {
        return this.tbyLayerConfigService.checkUrl(query.getId(), query.getUrl());
    }
}
